/*
 * Copyright (C), 2013-2014, 上海汽车集团股份有限公司
 * FileName: CookieUtil.java
 * Author:   xiejuan
 * Date:     2014年11月5日 下午6:03:09
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.jinznet.transit.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

/**
 * Cookie 工具类<br> 
 * 〈功能详细描述〉
 *
 * @author xiejuan
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class CookieUtil {
    /**OPENID*/
    public static final String OPENID="openid";
    
    /**OPENID 有效时间*/
    public static final int MAXAGE=365*24*3600;

	/**	获取cookie指定key的value	*/
	public static String getCookieByKey(HttpServletRequest request,final String key) {
		Cookie[] cookie = request.getCookies();
		String c_value = null;
		if(cookie!=null){
			for (int i = 0; i < cookie.length; i++) {
			Cookie cook = cookie[i];
			if(cook.getName().equalsIgnoreCase(key)){ //获取键 
				c_value=cook.getValue()+"";    //获取值 
				break;
			}
			}
		}
		return c_value;
	}
	
    /**
     * 设置cookie
     * 
     * @param response
     * @param name cookie名字
     * @param value cookie值
     * @param maxAge cookie生命周期 以秒为单位
     */
    public static void addCookie(HttpServletResponse response, String name, String value, int maxAge, String domian) {
        Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");
        if(!StringUtils.isEmpty(domian)){
            cookie.setDomain(domian);
        }
        if (maxAge > 0)
            cookie.setMaxAge(maxAge);
        response.addCookie(cookie);
    }
    
    /***
     * 修改特定cookie的value
     * @param request web请求对象
     * @param cookieKey cookieKey
     * @return
     */
    public static void updateCookie(HttpServletRequest request, HttpServletResponse response, String cookieKey, int maxAge, String domian, String value){
        if (StringUtils.isEmpty(cookieKey)) {
            Cookie[] cookies = request.getCookies();
            if (cookies != null && cookies.length > 0) {
                for (Cookie cookie : cookies) {
                    String name = cookie.getName();
                    if (cookieKey.equals(name)) {
                        cookie.setPath("/");
                        cookie.setDomain(domian);
                        cookie.setValue(value);
                        if(maxAge>0){
                            cookie.setMaxAge(maxAge);
                        }else{
                            cookie.setMaxAge(365*24*2600);
                        }
                        response.addCookie(cookie);
                        break;
                    }
                }
            }
        }
    }

    /**  
     * 清空cookie  
     */  
    public static void clearCookie(HttpServletRequest request,HttpServletResponse response) {
        Cookie[] cookies = request.getCookies(); 
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                cookie.setMaxAge(0);
                cookie.setPath("/");
                response.addCookie(cookie);
            }
        }
        
    }
    /***
     * 删除特定cookie的value
     * @param request web请求对象
     * @param cookieKey cookieKey
     * @return
     */
    public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String cookieKey, String domian){
        if (!StringUtils.isEmpty(cookieKey)) {
            Cookie[] cookies = request.getCookies();
            if (cookies != null && cookies.length > 0) {
                for (Cookie cookie : cookies) {
                    String name = cookie.getName();
                    if (cookieKey.equals(name)) {
                        cookie.setMaxAge(0);
                        cookie.setPath("/");
                        cookie.setDomain(domian);
                        response.addCookie(cookie);
                        break;
                    }
                }
            }
        }
    }
    /***
     * 
     * 功能描述:根据Cookie Name 获取<br>
     * 〈功能详细描述〉
     * 
     * @param request
     * @param name
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static Cookie getCookieByName(HttpServletRequest request, String name) {
        Map<String, Cookie> cookieMap = ReadCookieMap(request);
        if (cookieMap.containsKey(name)) {
            Cookie cookie = (Cookie) cookieMap.get(name);
            return cookie;
        } else {
            return null;
        }
    }

    /***
     * 
     * 功能描述: 读取cookie数据<br>
     * 〈功能详细描述〉
     * 
     * @param request
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private static Map<String, Cookie> ReadCookieMap(HttpServletRequest request) {
        Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }
}
